<?php

class FollowModel extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

    // 初始化粉丝
    public function initFollow($data) {
        $query = $this->db->get_where('wechat_follow', array('openid' => $data['openid']));
        $row = $query->num_rows();
        if ($row == 0) {
            // 粉丝第一次关注	
            $data['type'] = 1;
            $this->db->insert('wechat_follow', $data);
        } else {
            // 粉丝重新关注
            $this->db->where('openid', $data['openid']);
            $data['status'] = 1;
            $data['type'] = 1;
            $this->db->update('wechat_follow', $data);
        }
    }

    // 粉丝取消关注
    public function unSubscribe($openid) {
        $this->db->where('openid', $openid);
        $this->db->update('wechat_follow', array('status' => 0));
    }

    // 授权登录
    public function autoWechatReg($data = FALSE) {
        if ($data) {
            $query = $this->db->get_where('wechat_follow', array('openid' => $data['openid']));
            $row = $query->num_rows();
            if ($row == 0) {
                // 粉丝第一次授权	
                $follow['nickname'] = $data['nickname'];
                $follow['openid'] = $data['openid'];
                $follow['type'] = 2;
                $follow['status'] = 0;
                $follow['sex'] = $data['sex'];
                $follow['city'] = $data['city'];
                $follow['province'] = $data['province'];
                $follow['country'] = $data['country'];
                $follow['headimgurl'] = $data['headimgurl'];
                $this->db->insert('wechat_follow', $follow);
            }
        }
    }

}
